 package org.lagou.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lagou.Dao.ConnectionDao;
import org.lagou.Dao.impl.ConnectionDaoImpl;
import org.lagou.bean.ConnectionBean;
import org.lagou.bean.ReponseBody;
import org.lagou.tools.Tools;

import com.google.gson.Gson;

@WebServlet("/ConnectionServlet")
public class ConnectionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ConnectionDao dao=new ConnectionDaoImpl();
	Gson gson=new Gson();
       
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String type=request.getParameter("type");
		switch (type) {
		case "getConnectionAll":
			getConnectionAll(request,response);
			break;
		case "getConnectionAdd":
			getConnectionAdd(request,response);
			break;
		case "getConnectionUpdate":
			getConnectionUpdate(request,response);
			break;
		case "getConnectionDelete":
			getConnectionDelete(request,response);
			break;
		default:
			break;
		}
	
	}


	private void getConnectionDelete(HttpServletRequest request, HttpServletResponse response) {
		try {
			ReponseBody rb = new ReponseBody();
			int id = Integer.parseInt(request.getParameter("id"));
			rb.setData(dao.deleteConnection(id));
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
		}
	}


	private void getConnectionUpdate(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb=new ReponseBody();
		
		int id = Integer.parseInt(request.getParameter("id"));
		String logo=request.getParameter("logo");
		String introduce=request.getParameter("introduce");
		String  cooperate=request.getParameter("cooperate");
		String email1=request.getParameter("email1");
		String url=request.getParameter("url");
		String email3=request.getParameter("email3");
		String email2=request.getParameter("email2");
		String tel=request.getParameter("tel");
		String date = Tools.getNowDate();
		
		ConnectionBean info=new ConnectionBean(id, logo, introduce, cooperate, email1, url, email3, email2, tel, date);
		rb.setData(dao.addConnection(info));
		
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
		}
		
	}


	private void getConnectionAdd(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb=new ReponseBody();
		
		int id=Integer.parseInt(request.getParameter("id"));
		String logo=request.getParameter("logo");
		String introduce=request.getParameter("introduce");
		String  cooperate=request.getParameter("cooperate");
		String email1=request.getParameter("email1");
		String url=request.getParameter("url");
		String email3=request.getParameter("email3");
		String email2=request.getParameter("email2");
		String tel=request.getParameter("tel");
		String date = Tools.getNowDate();
		
		ConnectionBean info=new ConnectionBean(id, logo, introduce, cooperate, email1, url, email3, email2, tel, date);
		rb.setData(dao.addConnection(info));
		
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (Exception e) {
		}
	}


	private void getConnectionAll(HttpServletRequest request, HttpServletResponse response) {
		ReponseBody rb=new ReponseBody();
		rb.setData(dao.getConnectionAll());
		try {
			response.getWriter().write(gson.toJson(rb));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
